Dir_data=$1
cd $Dir_data

xprocessZero ( )
{
echo "===========xprocessZero==========="
if test ! -r Zero.fit
then
	echo "No Zero.fit"
	echo * | xargs -n 1 | grep  "bias" >aaa
	if test -s aaa
	then
		Dir_bias=`echo * | xargs -n 1 | grep  "bias" `
		cd $Dir_bias
		ls b*.fit >listbias
		xbiascom.sh listbias
		cp Zero.fit ../
		cd ../
	else
		echo "no any bias fit"
	fi
else
	echo "have Zero.fit"
fi

if test ! -r Dark.fit
then
	echo "=====No Dark.fit======"
        echo * | xargs -n 1 | grep  "dark" >aaa
        if test -s aaa
        then
		echo "have dark fit"
                Dir_dark=`echo * | xargs -n 1 | grep  "dark" `
                cd $Dir_dark
                ls dark*.fit >listdark
		if test -s ../Zero.fit
		then
			cp ../Zero.fit ./
			xzerocorr_list.sh listdark
		fi
		rm -rf Dark.fit
                xdarkcom.sh listdark
                cp Dark.fit ../
                cd ../
	else
		echo "no dark fit"
        fi
else
	echo "have Dark.fit"
fi
}

xprocessFlat ( )
{
if test ! -r Flat*.fit
then
	Dir_flat=`echo * | xargs -n 1 | grep  "flat" `
	echo * | xargs -n 1 | grep  "flat" >aaa
	if test -s aaa
	then
		if test -r Zero.fit 
		then
			cp Zero.fit $Dir_flat
		fi
		if test -r Dark.fit
		then
			cp Dark.fit $Dir_flat
		fi
			cd $Dir_flat
			ls f*R*.fit | grep -v "Zero.fit" | grep -v "Dark.fit" >listflat
			xzerocorr_list.sh listflat
			xdarkcorr_list.sh listflat
			xflatcom.sh listflat
			Flatimg=`ls Flat*.fit`
			if test -s $Flatimg
			then
				cp  $Flatimg ../
			fi	
			cd ../
			echo "flatcom is finished!"
	fi
fi
echo `pwd`
}

xprocessData ( )
{
echo "=========xprocessData======"
datafile=$1
if test -r $datafile
then
	Flatimg=`ls Flat*.fit`
	cp Zero.fit Dark.fit $Flatimg $datafile
	cd $datafile

#This part is for reprocessing the data. if needed plase reopen the next four lines.
        #rm -rf listdir.bak

	Dir_data=`pwd`
	echo G* | xargs -n 1 | grep -v "bias"  | grep -v "flat" | grep -v "dark"| grep -v "aaa" | grep -v "reduc" | grep -v "Zero" | grep -v "Dark" | grep -v "list" | grep -v "xtime" | grep -v "Flat" | grep -v "test" | awk '{print(Dirdata"/"$1)}' Dirdata=$Dir_data >listdir
	cat listdir | grep -v "listdir" >tmp
	mv tmp listdir
	if test -s listdir.bak
	then
		sort listdir.bak | uniq >tmp
		mv tmp listdir.bak
		diff listdir.bak listdir | grep  ">" | tr -d '>' | column -t >listmatch
		mv listmatch listdir
	fi
	cat listdir

	if test -r listdir_real
	then
		rm -rf listdir_real
	fi


	for Dir_singleobj in `cat listdir`
	do
		echo $Dir_singleobj
		cd $Dir_singleobj
		rm -rf *c.fit *c*s.fit
		rm -rf *.log
		Nimg=`ls *.fit | wc -l | awk '{print($1)}'`
		echo "Nimg=$Nimg"
#		echo "====================off line=========="
#		      cd $Dir_data
#                        Flatimg=`ls Flat*.fit`
#                        cp Zero.fit Dark.fit $Flatimg $Dir_singleobj
#                        echo $Dir_singleobj >>listdir_real
		#============================ On line ================
		if [ $Nimg  -ge 5  ]
		
		then
			cd $Dir_data
			Flatimg=`ls Flat*.fit`
			cp Zero.fit Dark.fit $Flatimg $Dir_singleobj
			echo $Dir_singleobj >>listdir_real
		elif [ $Nimg -ge 1  ]
		then
			echo "image number is >1"
			lastimg=`ls *.fit | tail -1` 
			dateobs=`gethead $lastimg "date-obs" | sed 's/T/ /g' | sed 's/:/ /g' | awk '{print($2+$3/60+$4/3600)}'`
			currentTime=`date -u | awk '{print($4)}' | sed 's/:/ /g' | awk '{print($1+$2/60+$3/3600)}'`	
			delta_time=`echo $currentTime $dateobs | awk '{print(($1-$2)*60)}'`
			echo "dateobs=$dateobs $currentTime  delta_time=$delta_time"
			if [ `echo " $delta_time > 10.0"  | bc ` -eq 1 ]
			then
				 cd $Dir_data
				 Flatimg=`ls Flat*.fit`
				 cp Zero.fit Dark.fit $Flatimg $Dir_singleobj
				 echo $Dir_singleobj >>listdir_real
				echo "good"
				 cat listdir_real
			fi
		fi
		#cd $Dir_singleobj
		#===================================================
	done
	
	rm -rf listdir
	sleep 3
	cd $Dir_data
	echo "~~~~~~~~~~~"
      	cat listdir_real | sort | uniq >tmp
	mv tmp listdir_real  
	#xzerodarkflatcorr_listdir.sh listdir_real
	xzeroflatcorr_listdir.sh listdir_real
	cd $Dir_data
	#xbmatch3_listdir.sh listdir_real
        xot_list.sh listdir_real	
	wait
	
	cat listdir_real >>listdir.bak
	cat listdir.bak | sort | uniq >tmp
	mv tmp listdir.bak
else
	echo "no $datafile"
fi
}

xprocessZero
xprocessFlat
rm -rf xtime aaa
# for test
#echo * | xargs -n 1 | grep -v bias | grep -v ark | grep -v lat | grep -v fit | grep -v aaa  | grep -v list >listdata
echo object >listdata
if test -s listdata
then
	echo "have data in listdata"
#	cat listdata
	for datafile in `cat listdata`
	do
		echo $datafile
		xprocessData $datafile
	done
else
	echo "no data in listdata"
fi
